package com.apollographql.apollo.subscription;

import com.apollographql.apollo.api.internal.json.JsonWriter;
import com.apollographql.apollo.api.internal.json.Utils;
import com.apollographql.apollo.subscription.OperationClientMessage;
import java.io.IOException;
import java.util.Map;
import com.kt.Metadata;
import com.kt.collections.MapsKt;
import com.kt.jvm.JvmStatic;
import com.kt.jvm.internal.DefaultConstructorMarker;
import com.kt.jvm.internal.Intrinsics;
import com.kt.text.StringsKt;
import okhttp3.HttpUrl;
import okio.Buffer;
import okio.BufferedSource;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000H\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010$\n\u0002\u0010\u000e\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\u0018\u0000 \u00162\u00020\u0001:\u0001\u0016B\u001b\u0012\u0014\u0010\u0002\u001a\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u00050\u0003¢\u0006\u0002\u0010\u0006J\u0010\u0010\u0007\u001a\u00020\b2\u0006\u0010\t\u001a\u00020\nH\u0016J\u0018\u0010\u000b\u001a\u00020\f2\u0006\u0010\r\u001a\u00020\u000e2\u0006\u0010\u000f\u001a\u00020\u0010H\u0016J\f\u0010\u0011\u001a\u00020\u0004*\u00020\u0012H\u0002J\u0014\u0010\u0013\u001a\u00020\f*\u00020\u00122\u0006\u0010\u0014\u001a\u00020\u0015H\u0002R\u001c\u0010\u0002\u001a\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u00050\u0003X\u0004¢\u0006\u0002\n\u0000¨\u0006\u0017"}, d2 = {"Lcom/apollographql/apollo/subscription/AppSyncOperationMessageSerializer;", "Lcom/apollographql/apollo/subscription/OperationMessageSerializer;", "authorization", "", "", "", "(Ljava/util/Map;)V", "readServerMessage", "Lcom/apollographql/apollo/subscription/OperationServerMessage;", "source", "Lokio/BufferedSource;", "writeClientMessage", "", "message", "Lcom/apollographql/apollo/subscription/OperationClientMessage;", "sink", "Lokio/BufferedSink;", "serializePayload", "Lcom/apollographql/apollo/subscription/OperationClientMessage$Start;", "writeTo", "writer", "Lcom/apollographql/apollo/api/internal/json/JsonWriter;", "Companion", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
/* compiled from: AppSyncOperationMessageSerializer.kt */
public final class AppSyncOperationMessageSerializer implements OperationMessageSerializer {
    public static final Companion Companion = new Companion(null);
    private static final String JSON_KEY_DATA = "data";
    private final Map<String, Object> authorization;

    @JvmStatic
    public static final String buildWebSocketUrl(String str, Map<String, ? extends Object> map) {
        return Companion.buildWebSocketUrl$default(Companion, str, map, null, 4, null);
    }

    @JvmStatic
    public static final String buildWebSocketUrl(String str, Map<String, ? extends Object> map, Map<String, ? extends Object> map2) {
        return Companion.buildWebSocketUrl(str, map, map2);
    }

    public AppSyncOperationMessageSerializer(Map<String, ? extends Object> map) {
        Intrinsics.checkParameterIsNotNull(map, "authorization");
        this.authorization = (Map<String, Object>) map;
    }

    /* JADX WARNING: Code restructure failed: missing block: B:10:0x002d, code lost:
        com.kt.io.CloseableKt.closeFinally(r0, r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:11:0x0030, code lost:
        throw r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:9:0x002c, code lost:
        r2 = move-exception;
     */
    @Override // com.apollographql.apollo.subscription.OperationMessageSerializer
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void writeClientMessage(OperationClientMessage r6, okio.BufferedSink r7) throws IOException {
        /*
            r5 = this;
            java.lang.String r0 = "message"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r6, r0)
            java.lang.String r0 = "sink"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r7, r0)
            boolean r0 = r6 instanceof com.apollographql.apollo.subscription.OperationClientMessage.Start
            if (r0 == 0) goto L_0x0031
            com.apollographql.apollo.api.internal.json.JsonWriter$Companion r0 = com.apollographql.apollo.api.internal.json.JsonWriter.Companion
            com.apollographql.apollo.api.internal.json.JsonWriter r0 = r0.of(r7)
            java.io.Closeable r0 = (java.io.Closeable) r0
            r1 = 0
            java.lang.Throwable r1 = (java.lang.Throwable) r1
            r2 = r0
            com.apollographql.apollo.api.internal.json.JsonWriter r2 = (com.apollographql.apollo.api.internal.json.JsonWriter) r2     // Catch:{ all -> 0x002a }
            r3 = 0
            r4 = r6
            com.apollographql.apollo.subscription.OperationClientMessage$Start r4 = (com.apollographql.apollo.subscription.OperationClientMessage.Start) r4     // Catch:{ all -> 0x002a }
            r5.writeTo(r4, r2)     // Catch:{ all -> 0x002a }
            com.kt.Unit r2 = com.kt.Unit.INSTANCE     // Catch:{ all -> 0x002a }
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            goto L_0x0044
        L_0x002a:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x002c }
        L_0x002c:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            throw r2
        L_0x0031:
            boolean r0 = r6 instanceof com.apollographql.apollo.subscription.OperationClientMessage.Init
            if (r0 == 0) goto L_0x0036
            goto L_0x003f
        L_0x0036:
            boolean r0 = r6 instanceof com.apollographql.apollo.subscription.OperationClientMessage.Stop
            if (r0 == 0) goto L_0x003b
            goto L_0x003f
        L_0x003b:
            boolean r0 = r6 instanceof com.apollographql.apollo.subscription.OperationClientMessage.Terminate
            if (r0 == 0) goto L_0x0044
        L_0x003f:
            com.apollographql.apollo.subscription.ApolloOperationMessageSerializer r0 = com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.INSTANCE
            r0.writeClientMessage(r6, r7)
        L_0x0044:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.subscription.AppSyncOperationMessageSerializer.writeClientMessage(com.apollographql.apollo.subscription.OperationClientMessage, okio.BufferedSink):void");
    }

    @Override // com.apollographql.apollo.subscription.OperationMessageSerializer
    public OperationServerMessage readServerMessage(BufferedSource source) throws IOException {
        Intrinsics.checkParameterIsNotNull(source, "source");
        return ApolloOperationMessageSerializer.INSTANCE.readServerMessage(source);
    }

    private final void writeTo(OperationClientMessage.Start $this$writeTo, JsonWriter writer) {
        try {
            writer.beginObject();
            writer.name("id").value($this$writeTo.subscriptionId);
            writer.name("type").value(OperationClientMessage.Start.TYPE);
            JsonWriter $this$writeObject$iv = writer.name("payload");
            $this$writeObject$iv.beginObject();
            $this$writeObject$iv.name("data").value(serializePayload($this$writeTo));
            JsonWriter $this$writeObject$iv2 = $this$writeObject$iv.name(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS);
            $this$writeObject$iv2.beginObject();
            $this$writeObject$iv2.name("authorization");
            Utils.writeToJson(this.authorization, writer);
            $this$writeObject$iv2.endObject();
            $this$writeObject$iv.endObject();
            writer.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    /* JADX WARNING: Code restructure failed: missing block: B:11:0x0040, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:12:0x0041, code lost:
        com.kt.io.CloseableKt.closeFinally(r5, r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0044, code lost:
        throw r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:17:0x0047, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0048, code lost:
        com.kt.io.CloseableKt.closeFinally(r0, r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x004b, code lost:
        throw r2;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    private final String serializePayload(OperationClientMessage.Start r14) {
        /*
            r13 = this;
            okio.Buffer r0 = new okio.Buffer
            r0.<init>()
            java.io.Closeable r0 = (java.io.Closeable) r0
            r1 = 0
            r2 = r1
            java.lang.Throwable r2 = (java.lang.Throwable) r2
            r3 = r0
            okio.Buffer r3 = (okio.Buffer) r3     // Catch:{ all -> 0x0045 }
            r4 = 0
            com.apollographql.apollo.api.internal.json.JsonWriter$Companion r5 = com.apollographql.apollo.api.internal.json.JsonWriter.Companion     // Catch:{ all -> 0x0045 }
            r6 = r3
            okio.BufferedSink r6 = (okio.BufferedSink) r6     // Catch:{ all -> 0x0045 }
            com.apollographql.apollo.api.internal.json.JsonWriter r5 = r5.of(r6)     // Catch:{ all -> 0x0045 }
            java.io.Closeable r5 = (java.io.Closeable) r5     // Catch:{ all -> 0x0045 }
            java.lang.Throwable r1 = (java.lang.Throwable) r1     // Catch:{ all -> 0x0045 }
            r6 = r5
            com.apollographql.apollo.api.internal.json.JsonWriter r6 = (com.apollographql.apollo.api.internal.json.JsonWriter) r6     // Catch:{ all -> 0x003e }
            r7 = 0
            r8 = r6
            r9 = 0
            r8.beginObject()     // Catch:{ all -> 0x003e }
            r10 = r8
            r11 = 0
            com.apollographql.apollo.subscription.ApolloOperationMessageSerializer r12 = com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.INSTANCE     // Catch:{ all -> 0x003e }
            r12.writePayloadContentsTo$apollo_runtime(r14, r6)     // Catch:{ all -> 0x003e }
            r8.endObject()     // Catch:{ all -> 0x003e }
            com.kt.Unit r6 = com.kt.Unit.INSTANCE     // Catch:{ all -> 0x003e }
            com.kt.io.CloseableKt.closeFinally(r5, r1)
            java.lang.String r1 = r3.readUtf8()
            com.kt.io.CloseableKt.closeFinally(r0, r2)
            return r1
        L_0x003e:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x0040 }
        L_0x0040:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r5, r1)
            throw r2
        L_0x0045:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x0047 }
        L_0x0047:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            throw r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.subscription.AppSyncOperationMessageSerializer.serializePayload(com.apollographql.apollo.subscription.OperationClientMessage$Start):java.lang.String");
    }

    @Metadata(bv = {1, 0, 3}, d1 = {"\u0000\u001c\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0003\n\u0002\u0010$\n\u0002\b\u0003\b\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J>\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0006\u001a\u00020\u00042\u0014\u0010\u0007\u001a\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u00010\b2\u0016\b\u0002\u0010\t\u001a\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u00010\bH\u0007J\u001a\u0010\n\u001a\u00020\u0004*\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u00010\bH\u0002R\u000e\u0010\u0003\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000¨\u0006\u000b"}, d2 = {"Lcom/apollographql/apollo/subscription/AppSyncOperationMessageSerializer$Companion;", "", "()V", "JSON_KEY_DATA", "", "buildWebSocketUrl", "baseWebSocketUrl", "authorization", "", "payload", "base64Encode", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
    /* compiled from: AppSyncOperationMessageSerializer.kt */
    public static final class Companion {
        @JvmStatic
        public final String buildWebSocketUrl(String str, Map<String, ? extends Object> map) {
            return buildWebSocketUrl$default(this, str, map, null, 4, null);
        }

        private Companion() {
        }

        public /* synthetic */ Companion(DefaultConstructorMarker $constructor_marker) {
            this();
        }

        /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: com.apollographql.apollo.subscription.AppSyncOperationMessageSerializer$Companion */
        /* JADX WARN: Multi-variable type inference failed */
        public static /* synthetic */ String buildWebSocketUrl$default(Companion companion, String str, Map map, Map map2, int i, Object obj) {
            if ((i & 4) != 0) {
                map2 = MapsKt.emptyMap();
            }
            return companion.buildWebSocketUrl(str, map, map2);
        }

        @JvmStatic
        public final String buildWebSocketUrl(String baseWebSocketUrl, Map<String, ? extends Object> map, Map<String, ? extends Object> map2) {
            Intrinsics.checkParameterIsNotNull(baseWebSocketUrl, "baseWebSocketUrl");
            Intrinsics.checkParameterIsNotNull(map, "authorization");
            Intrinsics.checkParameterIsNotNull(map2, "payload");
            String url = baseWebSocketUrl;
            if (StringsKt.startsWith(url, "ws://", true)) {
                url = "http" + StringsKt.drop(url, 2);
            } else if (StringsKt.startsWith(url, "wss://", true)) {
                url = "https" + StringsKt.drop(url, 3);
            }
            String url2 = HttpUrl.get(url).newBuilder().setQueryParameter("header", base64Encode(map)).setQueryParameter("payload", base64Encode(map2)).build().toString();
            Intrinsics.checkExpressionValueIsNotNull(url2, "url");
            if (StringsKt.startsWith(url2, "http://", true)) {
                return "ws" + StringsKt.drop(url2, 4);
            } else if (!StringsKt.startsWith(url2, "https://", true)) {
                return url2;
            } else {
                return "wss" + StringsKt.drop(url2, 5);
            }
        }

        private final String base64Encode(Map<String, ? extends Object> map) {
            Buffer buffer = new Buffer();
            try {
                Utils.writeToJson(map, JsonWriter.Companion.of(buffer));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return buffer.readByteString().base64Url();
        }
    }
}
